﻿using Furion.ConfigurableOptions;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FurStudy.Application
{
    #region 选项查找键流程

    /// <summary>
    /// AppInfoOptions强类型类
    /// 无[OptionsSettings]
    /// 以 Options 结尾，则键名为：AppInfo
    /// 【选项后期配置】 继承 IConfigurableOptions<TOptions, TOptionsValidation> 接口
    /// </summary>
    public class AppInfoOptions : IConfigurableOptions<AppInfoOptions,AppInfoValidation>
    {
        [Required(ErrorMessage = "名称不能为空")]
        public string Name { get; set; }
        [Required]
        public string Version { get; set; }
        [Required, MaxLength(100)]
        public string Company { get; set; }

        // 选项后期配置
        public void PostConfigure(AppInfoOptions options, IConfiguration configuration)
        {
            options.Name ??= "Furion";
            options.Version ??= "1.0.0";
            options.Company ??= "Baiqian";
        }
    }

    /// <summary>
    /// 选项更改通知（热更新）
    /// </summary>
    public class AppInfoOptions3 : IConfigurableOptionsListener<AppInfoOptions>
    {
        [Required(ErrorMessage = "名称不能为空")]
        public string Name { get; set; }
        [Required]
        public string Version { get; set; }
        [Required, MaxLength(100)]
        public string Company { get; set; }

        public void OnListener(AppInfoOptions options, IConfiguration configuration)
        {
            var name = options.Name; // 实时的最新值
            var version = options.Version; // 实时的最新值
        }

        public void PostConfigure(AppInfoOptions options, IConfiguration configuration)
        {
        }
    }

    /// <summary>
    /// 无[OptionsSettings]
    /// 不以 Options 结尾，则键名为：AppInfoSettings
    /// </summary>
    public class AppInfoSettings : IConfigurableOptions
    {
        public string Name { get; set; }
        public string Version { get; set; }
        public string Company { get; set; }
    }

    /// <summary>
    /// 有[OptionsSettings]
    /// 配置了 Path 属性，则键名为：AppSettings:AppInfo
    /// </summary>
    [OptionsSettings("AppSettings:AppInfo")]
    public class AppInfoOptions2 : IConfigurableOptions
    {
        public string Name { get; set; }
        public string Version { get; set; }
        public string Company { get; set; }
    }

    /// <summary>
    /// 有[OptionsSettings]
    /// 没有配置 Path 属性，，则键名为：AppInfoSettings
    /// </summary>
    [OptionsSettings]
    public class AppInfoSettings2 : IConfigurableOptions
    {
        public string Name { get; set; }
        public string Version { get; set; }
        public string Company { get; set; }
    }

    #endregion

}
